(function(){
	
	if (!window.CJ) {
		window.CJ = {};
	}

	function Billing () {
		this.name = 'Billing';
		this.bindEvent();
	}
	
	Billing.prototype = new EventCtl();
	Billing.prototype.bindEvent = function () {
		var self = this;
		$('#pop_buy ol').on('click', "li", function() {
			$(this).addClass('select focus').siblings().removeClass('select focus');
		});
		$('#pop_buy .ok').on('click', function() {
			var selectElem = $('#pop_buy ol .select');
			var billingType = selectElem.data('btype');
			var billingPrice = selectElem.data('price');
			CJ.plugin.billing.purchase(billingType, billingPrice, self.pid, self.eid, self.afterBillCB, self.back);
			$("#pop_buy").hide();
			$("#mask").hide();
		});
		$('#pop_buy .no').on('click', function() {
			self.back();
		});
		$('#pop_buy').on('mouseenter', "li", function() {
			$('#pop_buy li').removeClass('focus');
			$(this).addClass('focus');
		});
	};
	Billing.prototype.setContents = function (pid, eid, price, afterBillCB) {
		this.pid = pid;
		this.eid = eid;
		this.price = price;
		this.afterBillCB = afterBillCB;
		$('#single_price').text(this.price.single).parent('li').data('price', this.price.single);;
		$('#series_price').text(this.price.series).parent('li').data('price', this.price.series);;
		$('#term_price').text(this.price.term).parent('li').data('price', this.price.term);;
	};
	Billing.prototype.up = function () {
		var now = $('#pop_buy li.focus');
		if (now.parent('ol').length > 0) {
			var prev = now.prev();
			if (prev.length>0) {
				now.removeClass('focus');
				prev.addClass('focus');
			}
		} else {
			now.removeClass('focus');
			$('#pop_buy ol li:last-child').addClass('focus');
		}
	};
	Billing.prototype.down = function () {
		var now = $('#pop_buy li.focus');
		if (now.parent('ol').length > 0) {
			var next = now.next();
			now.removeClass('focus');
			if (next.length>0) {
				next.addClass('focus');
			} else {
				$('#pop_buy .ok').addClass('focus');
			}
		}
	};
	Billing.prototype.left = function () {
		var now = $('#pop_buy li.focus');
		if (now.parent('ul').length > 0) {
			var prev = now.prev();
			if (prev.length>0) {
				now.removeClass('focus');
				prev.addClass('focus');
			}
		}
	};
	Billing.prototype.right = function () {
		var now = $('#pop_buy li.focus');
		if (now.parent('ul').length > 0) {
			var next = now.next();
			if (next.length>0) {
				now.removeClass('focus');
				next.addClass('focus');
			}
		}
	};
	Billing.prototype.back = function () {
		$("#pop_buy").hide();
		$("#mask").hide();
		CJ.Scene.setDelegate(CJ.Scene["Episode"]);
	};
	Billing.prototype.enter = function () {
		var now = $('#pop_buy .focus');
		if (now.parent('ul').length > 0) {
			if (now.hasClass('ok')) {
				var selectElem = $('#pop_buy ol .select');
				var billingType = selectElem.data('btype');
				var billingPrice = selectElem.data('price');
				CJ.plugin.billing.purchase(billingType, billingPrice, this.pid, this.eid, this.afterBillCB, this.back);
				$("#pop_buy").hide();
				$("#mask").hide();
			} else {
				this.back();
			}
		} else {
			now.addClass('select').siblings().removeClass('select');
		}
	};

	$.extend(CJ.Scene, {
							billing: function(pid, eid, price, afterBillCB){
								this["Billing"].setContents(pid, eid, price, afterBillCB);
								$('#pop_buy .focus, #pop_buy .select').removeClass('select focus');
								$('#pop_buy ol li:first-child').addClass('select focus');
								$("#pop_buy").show();
								$("#mask").show();
								this.setDelegate(this["Billing"]);
							}
						});

	$(document).ready(function(){
		CJ.Scene["Billing"] = new Billing();
	});

})();